#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=1e5+10;
int n,rt;
struct FHQTreap
{
	int val[MAXN],pri[MAXN],siz[MAXN],c[MAXN][2],cnt;
	inline int newnode(int x)
	{
		val[++cnt]=x;
		pri[cnt]=rand();
		siz[cnt]=1;
		return cnt;
	}
	inline void push_up(int x)
	{
		siz[x]=siz[c[x][0]]+siz[c[x][1]]+1;
		return;
	}
	int merge(int x,int y)
	{
		if(!x||!y)return x+y;
		if(pri[x]<pri[y])
		{
			c[x][1]=merge(c[x][1],y);
			push_up(x);
			return x;
		}
		else
		{
			c[y][0]=merge(x,c[y][0]);
			push_up(y);
			return y;
		}
	}
	void split(int o,int k,int &x,int &y)
	{
		if(!o)
		{
			x=y=0;
			return;
		}
		if(val[o]<=k)
			x=o,split(c[o][1],k,c[o][1],y);
		else
			y=o,split(c[o][0],k,x,c[o][0]);
		push_up(o);
		return;
	}
	int find(int o,int k)
	{
		if(k<=siz[c[o][0]])return find(c[o][0],k);
		if(k<=siz[c[o][0]]+1)return o;
		return find(c[o][1],k-siz[c[o][0]]-1);
	}
	void solve()
	{
		while(n--)
		{
			int op=read(),a=read(),x,y,z;
			if(op==1)
			{
				split(rt,a,x,y);
				rt=merge(merge(x,newnode(a)),y);
			}
			else if(op==2)
			{
				split(rt,a,x,z);
				split(x,a-1,x,y);
				rt=merge(merge(x,merge(c[y][0],c[y][1])),z);
			}
			else if(op==3)
			{
				split(rt,a-1,x,y);
				printf("%d\n",siz[x]+1);
				rt=merge(x,y);
			}
			else if(op==4)
				printf("%d\n",val[find(rt,a)]);
			else if(op==5)
			{
				split(rt,a-1,x,y);
				printf("%d\n",val[find(x,siz[x])]);
				rt=merge(x,y);
			}
			else
			{
				split(rt,a,x,y);
				printf("%d\n",val[find(y,1)]);
				rt=merge(x,y);
			}
		}
		return;
	}
}T1;
int main()
{
	n=read();
	srand(n);
	T1.solve();
	return 0;
}
